package com.teziyou.commodity.logic;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mytravel.platform.bean.DictionaryBean;
import com.teziyou.commodity.compartor.DictionaryCompartor;
import com.teziyou.commodity.enumtype.CategoryEnum;
import com.teziyou.commodity.service.DictService;
import com.teziyou.commodity.util.Lists;
import com.teziyou.commodity.util.Maps;

@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class CategoryLogic {
	
	@Autowired
	private DictService dictService;
	
	public List getCategoryTree(int code) {
		System.out.println(code);
		List<DictionaryBean> list = dictService.getDictionaryList("0220");
		Collections.sort(list, new DictionaryCompartor());
		List treeList = new ArrayList();
		//List ids = new ArrayList();
		List excepts = new ArrayList();
		List roots = new ArrayList();
		if(code==CategoryEnum.TICKET.getCode()) {
			excepts = Lists.newArrayList(140, 141);
			roots = Lists.newArrayList(140, 141);
		} else if(code==CategoryEnum.LINE.getCode()) {
			excepts = Lists.newArrayList(139, 141);
			roots = Lists.newArrayList(139, 141);
		} else if(code == CategoryEnum.SCENETHEME.getCode()) {//主题景区分类
			excepts = Lists.newArrayList(1, 207210);//这个作用是什么？
			roots = Lists.newArrayList(1, 207210);
		}
		for(DictionaryBean bean:list) {
			//System.err.println(bean);
			int id = bean.getDictionaryId();
			
			int pid = bean.getDicDictionaryId();
			if(excepts.contains(pid)||roots.contains(id)){
				excepts.add(id);
				continue;
			}
			String name = bean.getName();
			//ids.add(id);
			//Map dict = JSON.parseObject(JSON.toJSONString(bean));
			//Map dict = Maps.newHashMap("id", id, "pId", pid, "name", name);
			//if(ids.contains(pid)) dict.put("open", true);
			treeList.add(Maps.newHashMap("id", id, "pId", pid, "name", name, "open", true));
		}
		//treeList.add(Maps.newHashMap("id", 0, "pId", -1, "name", "根分类", "open", true));
		System.err.println(treeList);
		return treeList;
	}
	
	public void saveCategory(DictionaryBean dict) {
		if(dict.getDictionaryId()==0) dictService.insertDictionaryTrift(dict);
		else dictService.updateDictionaryTrift(dict);
	}

}
